<template>
	<view class="content" :style="colorStyle">
		<view class="" @click="upLoadOneImg">
			单图上传
		</view>
		<view class="" @click="uploadImageTwo">
			多图上传
		</view>
		<!-- 可拖拽功能 -->
		<view class="img_box">
			<xiaokeaiDragImageVue :imgPadding="0" :colNum="3" :enableDel="true" :imgList.sync="coverImg"
				@moveEndList="moveEndList" @delImg="delImg">
				<view class="list" @click="uploadImageTwo" v-if="coverImg.length<opt.count">
					<view class="img">
						多图可拖拽上传
					</view>
				</view>
			</xiaokeaiDragImageVue>
		</view>
		<!-- 自定义tabBar占位符 -->
		<view class="tabBar_bottom"></view>
		<tabBar></tabBar>
		<!-- 限制输入框大于0的小数且保留两位小数 -->
		<input type="digit" placeholder="请输入数字" :maxlength="inputMaxlength" @input="changeWeight" />
		<!-- 常用组件 -->
		<!-- 公告滚动通知 -->
		<noticeVue :noticeList="noticeList" v-if="noticeList.length"></noticeVue>
		<!-- 倒计时 -->
		<countDown colors="#D43030" :hourText="`时`" :minuteText="`分`" :secondText="`秒`" datatime="时间戳"
			:isDay="false" @endTime="endTime"></countDown>
		<emptyPage text="暂无列表"></emptyPage>
		<!-- 获取验证码 -->
		<view class="input-box">
			<view class="name">验证码</view>
			<view class="value">
				<input v-model="code" placeholder="'请输入验证码'" />
				<button class="code" :disabled="disabled" @click="getCode">
					{{ text }}
				</button>
			</view>
		</view>
	</view>
</template>

<script>
	import xiaokeaiDragImageVue from "@/components/xiaokeai-dragImage/xiaokeai-dragImage.vue";
	import sendVerifyCode from "@/mixins/SendVerifyCode";
	import colors from "@/mixins/color.js"
	import {
		getUserInfo
	} from '@/api/user';
	import {
		mapGetters
	} from "vuex";
	import noticeVue from "@/components/notice/notice.vue"
	import emptyPage from "@/components/emptyPage.vue";
	import countDown from '@/components/countDown';
	import modal from "@/components/modal/modal.vue";
	import {
		requestAndroidPermission
	} from "@/utils/validate.js"
	export default {
		computed: mapGetters(['isLogin']),
		mixins: [colors, sendVerifyCode],
		components: {
			noticeVue,
			emptyPage,
			countDown,
			modal,
			xiaokeaiDragImageVue
		},
		watch: {
			isLogin: {
				handler(nVal, oVal) {
					if (nVal) {
						// 业务
					}
				},
				deep: true //深度监听设置为 true
			}
		},
		data() {
			return {
				title: 'Hello',
				opt: {
					count: 3,
					url: '/data/api.auth.center/upload'
				},
				noticeList: [],
				coverImg: []
			}
		},
		onLoad() {
			this.getUserInfo()
			// #ifdef H5
			this.setToken()
			// #endif
		},
		methods: {
			moveEndList(e) {
				console.log('移动结束后的新图片列表');
				console.log(e);
				this.coverImg = e.map((item) => {
					return {
						url: item.url
					}
				})
			},
			delImg(e) {
				console.log('删除图片点击事件');
				console.log(e);
				this.coverImg.splice(e, 1)
			},
			getCode() {
				if (!this.disabled) {
					this.sendCode() //60秒倒计时
					const params = {
						email: this.email,
						type: this.type
					}
					fetch(params) //发送验证码
				}
			},
			endTime() {
				console.log('时间到了');
			},
			changeWeight(e) {
				let that = this
				this.checkNum(e).then((value) => {
					// 业务逻辑
					this.$nextTick(function() {
						// 输入框的值 = value
					})
				})
			},
			// 常用方法this.$L.getLocation()
			// 滚动加载
			getData() {
				if (this.loadend) return;
				if (this.loading) return;
				this.loading = true;
				this.loadTitle = `加载更多`;

				fetch().then((res) => {
					let list = res.data.list || []
					this.loadend = list.length < this.limit
					this.dataList = this.dataList.concat(list)
					this.loading = false;
					this.loadTitle = this.loadend ? `没有更多内容啦~` : `加载更多`;
					this.page++;
				}).catch(err => {
					this.loading = false;
					this.loadTitle = `加载更多`;
				});
			},
			getUserInfo() {
				console.log(getUserInfo, "请求接口");
				getUserInfo().then((res) => {
					console.log(res, "返回信息");
				}).catch(err => {
					console.log(err, "错误信息");
				});
			},
			setToken() {
				const token = uni.getStorageSync("token")
				// #ifdef H5
				var url = window.location.href
				var theRequest = new Object();
				if (url.indexOf("?") != -1) {
					var str = url.substr(url.indexOf("?") + 1);
					var strs = str.split("&");
					for (var i = 0; i < strs.length; i++) {
						theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
					}
				}
				if (theRequest.pid) {
					var pid = theRequest.pid.replace("#/", "");
					uni.setStorageSync('pid', pid)
				}
				if (theRequest.code && !token) {
					let that = this;
					// 调用授权接口
					this.$http.oauth({
						code: theRequest.code,
						state: 'wxc926fae26dd9d503'
					}).then((res) => {
						uni.setStorageSync('token', res.data.token)
						uni.setStorageSync('openid', res.data.user.openid)
					})
				}
				// #endif
			},
			upLoadOneImg() {
				// #ifdef APP
				requestAndroidPermission('android.permission.CAMERA').then((status) => {
					if (status == 1) {
						this.$util.uploadImageTwo(this.imgOpt, (res) => {
							this.imgArr.push(res.data.url)
						})
					} else {
						this.$util.Tips({
							title: this.$t("请允许程序访问相机")
						})
					}
				})
				// #endif
				// #ifdef MP||H5
				this.$util.uploadImageOne('/data/api.auth.center/upload', (res) => {
					const data = res.data
					console.log(data, "成功");
				}, (err) => {
					console.log(err, "失败");
				})
				// #endif

			},
			uploadImageTwo() {
				this.$util.uploadImageTwo(this.opt, (res) => {
					// const data = res.data
					this.coverImg.push({url:res.data.url})
				}, (err) => {
					console.log(err, "失败");
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;

		.img_box {
			.img {
				width: 190rpx;
				height: 190rpx;
				display: inline-block;
				vertical-align: top;
				border-radius: 10rpx;
				overflow: hidden;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>